﻿function RRectZoominTool(tool){
    if(!tool){return;}
	this.tool=tool; 
	this.maplet=null;
	this.marker=null;	
	this.leftbottom=null;
	this.righttop=null;
	this.bounds=null;
	this.isLock=true;
	this.isUseing=false;
	this.isKeepOtherMarker=false;
	
	if(tool.src){
	    this.imgpath=this.tool.src;
	}
	
	this.toString=function(){
		return 'RRectZoominTool';
	}

	this.turnon=function(){	 
	    //创建监听
	    this.addEvt();    
	    if(this.isLock){	               
	        return;
	    }
	    this.notify('onturnon');
	    this.maplistener=new Object();
		this.maplistener.root=this;
		this.maplet.setCursor('crosshair');
		this.maplistener.onmousedown=function(){
		    this.root.notify('onbegin');
		    this.root.maplet.setCursor('crosshair');
			this.root.isUseing=true;
			this.root.maplet.disableDrag();
			var left=this.root.maplet.mouseX-getBoundingClientRect((this.root.maplet.get().parentElement||this.root.maplet.get().parentNode)).left;
			var top=this.root.maplet.mouseY-getBoundingClientRect((this.root.maplet.get().parentElement||this.root.maplet.get().parentNode)).top;
			this.root.leftbottom=this.root.maplet.getLatLng(left,top);
			this.root.marker=this.root.maplet.addStaticRectRegionMarker(new RBounds(this.root.leftbottom,this.root.leftbottom));
		}
		this.maplistener.onmousemoved=function(){
			if(this.root.isUseing){
				this.root.maplet.setCursor('crosshair');
				var left=this.root.maplet.mouseX-getBoundingClientRect((this.root.maplet.get().parentElement||this.root.maplet.get().parentNode)).left;
				var top=this.root.maplet.mouseY-getBoundingClientRect((this.root.maplet.get().parentElement||this.root.maplet.get().parentNode)).top;
				this.root.marker.righttop=this.root.maplet.getLatLng(left,top);
				this.root.maplet.refreshMarkers();
			}
		}
		this.maplistener.onmouseup=function(){
		    var left=this.root.maplet.mouseX-getBoundingClientRect((this.root.maplet.get().parentElement||this.root.maplet.get().parentNode)).left;
			var top=this.root.maplet.mouseY-getBoundingClientRect((this.root.maplet.get().parentElement||this.root.maplet.get().parentNode)).top;
			this.root.righttop=this.root.maplet.getLatLng(left,top);
            this.root.marker.destroy();
			this.root.maplet.setCursor('crosshair');
			this.root.maplet.enableDrag();
			this.root.isUseing=false;
			this.root.notify('onsuccess');
			if(this.root.leftbottom.lat!=this.root.righttop.lat||this.root.leftbottom.lng!=this.root.righttop.lng){//alert(this.root.maplet.zoomlevels);
			    if(this.root.maplet.zoom  == this.root.maplet.availableMaxZoomLevel)
			    {
			        this.root.maplet.isReturnboundsCenter=true;//
			        this.root.maplet.getMaxLatLngFromArray(new Array(this.root.leftbottom,this.root.righttop),false);
			        this.root.maplet.isReturnboundsCenter=false;//
			        this.root.maplet.smoothMove(this.root.maplet.boundsCenter.lat, this.root.maplet.boundsCenter.lng);
			    }
			    else
			    {
	        	    this.root.maplet.dynSetMaxBoundsByLatLngs(new Array(this.root.leftbottom,this.root.righttop),false,-70);	
			    }
			}			
		}
		this.maplet.addListener(this.maplistener,'onmousedown');
		this.maplet.addListener(this.maplistener,'onmousemoved');
		this.maplet.addListener(this.maplistener,'onmouseup');
	}
	
	this.turnoff=function(){
	    this.isLock=true;
	    this.maplet.setCursor('default');
	    this.notify('onturnoff');
	    if(this.maplistener){
			this.maplistener.onmousedown=function(){};
			this.maplistener.onmousemoved=function(){};
			this.maplistener.onmouseup=function(){};
		}
		if(this.imgpath){
		    this.tool.src=this.imgpath;
		}		
	}
	this.clear=function(){};
	
	
	this.setMaplet=function(maplet){ 
		this.maplet=maplet;		
	} 
	
	
	this.onbegin=new Array();
	this.onsuccess=new Array();
	this.onturnon=new Array();
	this.onturnoff=new Array();
	
	this.addListener=function(listener,estring){
		switch(estring){
			case 'onbegin':
 				if(listener.onbegin){
 					this.onbegin[this.onbegin.length]=listener;
 				} else{
 					window.status=this.toString()+':不合法的'+estring+'事件对象';
 				}
			break;
			case 'onsuccess':
 				if(listener.onsuccess){
 					this.onsuccess[this.onsuccess.length]=listener;
 				} else{
 					window.status=this.toString()+':不合法的'+estring+'事件对象';
 				}
			break;
			case 'onturnon':
 				if(listener.onturnon){
 					this.onturnon[this.onturnon.length]=listener;
 				} else{
 					window.status=this.toString()+':不合法的'+estring+'事件对象';
 				}
			break;
			case 'onturnoff':
 				if(listener.onturnoff){
 					this.onturnoff[this.onturnoff.length]=listener;
 				} else{
 					window.status=this.toString()+':不合法的'+estring+'事件对象';
 				}
			break;
			default:
				window.status=this.toString()+'类不存在'+estring+'事件';
			break;
		}
	}
	
	this.notify=function(estring){
		switch(estring){
			case 'onbegin':
 				for(var i=0;i<this.onbegin.length;i++){
 					if(this.onbegin[i].onbegin){
 						this.onbegin[i].onbegin();
 					}else{ 
 						window.status=this.toString()+'类的某'+estring+'事件监听对象损坏';
 					}
 				}
			break;
			case 'onsuccess':
 				for(var i=0;i<this.onsuccess.length;i++){
 					if(this.onsuccess[i].onsuccess){
 						this.onsuccess[i].onsuccess();
 					}else{ 
 						window.status=this.toString()+'类的某'+estring+'事件监听对象损坏';
 					}
 				}
			break;
			case 'onturnon':
 				for(var i=0;i<this.onturnon.length;i++){
 					if(this.onturnon[i].onturnon){
 						this.onturnon[i].onturnon();
 					}else{ 
 						window.status=this.toString()+'类的某'+estring+'事件监听对象损坏';
 					}
 				}
			break;
			case 'onturnoff':
 				for(var i=0;i<this.onturnoff.length;i++){
 					if(this.onturnoff[i].onturnoff){
 						this.onturnoff[i].onturnoff();
 					}else{ 
 						window.status=this.toString()+'类的某'+estring+'事件监听对象损坏';
 					}
 				}
			break;
			default:
				window.status=this.toString()+'类不存在'+estring+'事件';
			break;
		}
	}

    this.keydown = function(evt)
	{
	    if((evt || window.event).keyCode == 27)
	    {
	        this.turnoff();
	        this.delEvt();
	        if(this.marker)
	        {
                this.marker.destroy();
            }
            this.maplet.enableDrag();
	    }
	}
   
    //创建一个Esc建的监听事件	
    this.addEvt = function ()
    {  
	    if(window.event) //IE
	    {
	        g_addListener("keydown",this.keydown,this);
	        document.attachEvent("onkeydown",g_notify);
        }
        else  //Firefox
        {
            g_addListener("keydown",this.keydown,this);
	        document.addEventListener("keydown",g_notify,false);
        }
    }
     
    //销毁ESC键的监听事件   
    this.delEvt = function()
    {
        if (document.attachEvent)
        {
            document.detachEvent("onkeydown",g_notify);
        } 
        else
        {
            document.removeEventListener("keydown",g_notify,false);          
        }
    }	
}
